<template xlang="wxml" minapp="mpvue">
	<view class="tki-tree" @touchmove.stop="">
		<view class="tki-tree-mask" :class="{'show':showTree}" @tap="_cancel"></view>
		<view class="tki-tree-cnt" :class="{'show':showTree}">
			<view class="tki-tree-bar">
				<view class="tki-tree-bar-cancel" :style="{'color':cancelColor}" hover-class="hover-c" @tap="_cancel">取消</view>
				<view class="tki-tree-bar-title" :style="{'color':titleColor}">{{title}}</view>
				<view class="tki-tree-bar-confirm" :style="{'color':confirmColor}" hover-class="hover-c" @tap="_confirm">确定</view>
			</view>
			<view class="tki-tree-view">
				<scroll-view class="tki-tree-view-sc" :scroll-y="true">
					<block v-for="(item, index) in treeList" :key="index">
						<view class="tki-tree-item" :style="[{
							paddingLeft: item.rank*15 + 'px',
							zIndex: item.rank*-1 +50
						}]"
						 :class="{
							border: border === true,
							show: item.show,
							last: item.lastRank,
							showchild: item.showChild
						}">
							<view class="tki-tree-label" @tap.stop="_treeItemTap(item, index)">
								<image class="tki-tree-icon" :src="item.lastRank ? lastIcon : item.showChild ? currentIcon : defaultIcon"></image>
								{{item.text}}
							</view>
							<view class="tki-tree-check" @tap.stop="_treeItemSelect(item, index)" v-if="selectParent?true:item.lastRank">
								<view class="tki-tree-check-yes" v-if="item.checked" :class="{'radio':!multiple}" :style="{'border-color':confirmColor}">
									<view class="tki-tree-check-yes-b" :style="{'background-color':confirmColor}"></view>
								</view>
								<view class="tki-tree-check-no" v-else :class="{'radio':!multiple}" :style="{'border-color':confirmColor}"></view>
							</view>
						</view>
					</block>
				</scroll-view>
			</view>
		</view>
	</view>
</template>

<script>
	import config from '@/common/js/config.js'
	import helper  from '@/common/js/helper.js'
	export default {
		name: "tki-tree",
		props: {
			range: {
				type: Array,
				default: function() {
					return []
				}
			},
			rangeKey: {
				type: String,
				default: 'label'
			},
			title: {
				type: String,
				default: ''
			},
			multiple: { // 是否可以多选
				type: Boolean,
				default: false
				// default: true
			},
			selectParent: { //是否可以选父级
				type: Boolean,
				default: false
			},
			confirmColor: { // 确定按钮颜色
				type: String,
				default: '' // #07bb07
			},
			cancelColor: { // 取消按钮颜色
				type: String,
				default: '' // #757575
			},
			titleColor: { // 标题颜色
				type: String,
				default: '' // #757575
			},
			// currentIcon: { // 展开时候的ic
			// 	type: String,
			// 	default: '../../static/image/currentIcon.png'
			// },
			// defaultIcon: { // 折叠时候的ic
			// 	type: String,
			// 	default: '../../static/image/defaultIcon.png'
			// },
			lastIcon: { // 没有子集的ic
				type: String,
				default: ''
			},
			border: { // 是否有分割线
				type: Boolean,
				default: false
			},
		},
		data() {
			return {
				showTree: false,
				treeList: [],
				selectIndex: -1,
				currentIcon:config.Resources.common.currentIcon,
				defaultIcon:config.Resources.common.defaultIcon
			}
		},
		computed: {},
		methods: {
			/* 显示 */
			_show() {
				this.showTree = true
			},
			/* 隐藏 */
			_hide() {
				this.showTree = false
			},
			/* 点击取消 */
			_cancel() {
				this._hide()
				this.$emit("cancel", '');
			},
			/* 点击 */
			_confirm() {
				// 处理所选数据
				let rt = [],
					obj = {};
				this.treeList.forEach((v, i) => {
					if (this.treeList[i].checked) {
						obj = {}
						obj.parents = this.treeList[i].parents
						obj = Object.assign(obj, this.treeList[i].source)
						// 移除子元素
						delete obj.ChildNodes
						rt.push(obj)
					}
				})
				this._hide()
				if (rt.length > 0) {
					this.$eventHub.$emit("treemodality", rt);
				} else {
					helper.msg('未选择!');
				}
			},
			//扁平化树结构
			_renderTreeList(list = [], rank = 0, parentId = [], parents = []) {
				list.forEach(item => {
					this.treeList.push({
						id: item.id,
						text: item[this.rangeKey],
						source: item,
						parentId, // 父级id数组
						parents, // 父级id数组
						rank, // 层级
						showChild: false, //子级是否显示
						show: rank === 0, // 自身是否显示
						checked: false,
					})
					if (Array.isArray(item.ChildNodes) && item.ChildNodes.length > 0) {
						// // console.log(item)
						let parentid = [...parentId],
							parentArr = [...parents];
						delete parentArr.ChildNodes
						parentid.push(item.id);
						parentArr.push({
							id: item.id,
							[this.rangeKey]: item[this.rangeKey]
						})
						this._renderTreeList(item.ChildNodes, rank + 1, parentid, parentArr);
					} else {
						this.treeList[this.treeList.length - 1].lastRank = true;
					}
				})
			},
			// 点击
			_treeItemTap(item, index) {
				if (item.lastRank === true) {
					//点击最后一级时触发事件
					this.treeList[index].checked = !this.treeList[index].checked
					this._fixMultiple(index)
					return;
				}
				let list = this.treeList;
				let id = item.id;
				item.showChild = !item.showChild;
				list.forEach((childItem, i) => {
					if (item.showChild === false) {
						//隐藏所有子级
						if (!childItem.parentId.includes(id)) {
							return;
						}
						if (childItem.lastRank !== true) {
							childItem.showChild = false;
						}
						childItem.show = false;
					} else {
						if (childItem.parentId[childItem.parentId.length - 1] === id) {
							childItem.show = true;
						}
					}
				})
				// // console.log(this.treeList)
			},
			_treeItemSelect(item, index) {
				this.treeList[index].checked = !this.treeList[index].checked
				this._fixMultiple(index)
			},
			// 处理单选多选
			_fixMultiple(index) {
				if (!this.multiple) {
					// 如果是单选
					this.treeList.forEach((v, i) => {
						if (i != index) {
							this.treeList[i].checked = false
						}
					})
				}
			},
			// 重置数据
			_reTreeList() {
				this.treeList.forEach((v, i) => {
					this.treeList[i].checked = false
				})
			}
		},
		watch: {
			range(list) {
				this.treeList.splice(0);
				this._renderTreeList(list);
			},
			multiple() {
				if (this.range.length) {
					this._reTreeList();
				}
			},
			selectParent() {
				if (this.range.length) {
					this._reTreeList();
				}
			},
		},
		mounted() {}
	}
</script>

<style>
	.tki-tree-mask {
		position: fixed;
		top: 0upx;
		left: 0upx;
		right: 0upx;
		bottom: 0upx;
		z-index: 9998;
		background-color: rgba(0, 0, 0, 0.6);
		opacity: 0;
		transition: all 0.3s ease;
		visibility: hidden;
	}

	.tki-tree-mask.show {
		visibility: visible;
		opacity: 1;
	}

	.tki-tree-cnt {
		position: fixed;
		top: 0upx;
		left: 0upx;
		right: 0upx;
		bottom: 0upx;
		z-index: 9999;
		top: 652upx;
		transition: all 0.3s ease;
		transform: translateY(100%);
	}

	.tki-tree-cnt.show {
		transform: translateY(0);
	}

	.tki-tree-bar {
		background-color: #fff;
		height: 88upx;
		padding-left: 20upx;
		padding-right: 20upx;
		display: flex;
		justify-content: space-between;
		align-items: center;
		box-sizing: border-box;
		border-bottom-width: 1upx !important;
		border-bottom-style: solid;
		border-bottom-color: #c8c7cc;
		font-size: 32upx;
		color: #757575;
		line-height: 1;
	}

	.tki-tree-bar-confirm {
		color: #4869EE;
	}

	.tki-tree-bar-cancel {
		color: #9A9A9A;
	}

	.tki-tree-view {
		position: absolute;
		top: 0upx;
		left: 0upx;
		right: 0upx;
		bottom: 0upx;
		top: 88upx;
		background-color: #fff;
		padding-top: 20upx;
		padding-right: 20upx;
		padding-bottom: 20upx;
		padding-left: 20upx;
	}

	.tki-tree-view-sc {
		height: 100%;
		overflow: hidden;
	}

	.tki-tree-item {
		display: flex;
		justify-content: space-between;
		align-items: center;
		font-size: 26upx;
		color: #757575;
		line-height: 1;
		height: 0;
		opacity: 0;
		transition: 0.2s;
		position: relative;
		overflow: hidden;
	}

	.tki-tree-item.show {
		height: 80upx;
		opacity: 1;
	}

	.tki-tree-item.showchild:before {
		transform: rotate(90deg);
	}

	.tki-tree-item.last:before {
		opacity: 0;
	}

	.tki-tree-icon {
		width: 26upx;
		height: 26upx;
		margin-right: 8upx;
	}

	.tki-tree-label {
		flex: 1;
		display: flex;
		align-items: center;
		height: 100%;
		line-height: 1.2;
	}

	.tki-tree-check {
		width: 40px;
		height: 40px;
		display: flex;
		justify-content: center;
		align-items: center;
	}

	.tki-tree-check-yes,
	.tki-tree-check-no {
		width: 20px;
		height: 20px;
		border-top-left-radius: 20%;
		border-top-right-radius: 20%;
		border-bottom-right-radius: 20%;
		border-bottom-left-radius: 20%;
		border-top-width: 1upx;
		border-left-width: 1upx;
		border-bottom-width: 1upx;
		border-right-width: 1upx;
		border-style: solid;
		border-color: #07bb07;
		display: flex;
		justify-content: center;
		align-items: center;
		box-sizing: border-box;
	}

	.tki-tree-check-yes-b {
		width: 12px;
		height: 12px;
		border-top-left-radius: 20%;
		border-top-right-radius: 20%;
		border-bottom-right-radius: 20%;
		border-bottom-left-radius: 20%;
		background-color: #07bb07;
	}

	.tki-tree-check .radio {
		border-top-left-radius: 50%;
		border-top-right-radius: 50%;
		border-bottom-right-radius: 50%;
		border-bottom-left-radius: 50%;
	}

	.tki-tree-check .radio .tki-tree-check-yes-b {
		border-top-left-radius: 50%;
		border-top-right-radius: 50%;
		border-bottom-right-radius: 50%;
		border-bottom-left-radius: 50%;
	}

	.hover-c {
		opacity: .6;
	}
</style>
